package com.ironone.streaming.model;

import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import com.ironone.streaming.application.FormObject;

public class ModelMapper {
	// getInitializeSvcDataModel
	public static String getGeneralDataModel(ResponseDataModel rdm){
		try {
			if(rdm.responseData.getString("token") != null && rdm.responseData.getString("token") != ""){
				FormObject.getInstance().setAccessToken(rdm.responseData.getString("token"));
			}

			// NOTE: server_time == data_version (1st launch of the app data_version == 1)
			if(rdm.responseData.getString("server_time") != null && rdm.responseData.getString("server_time") != ""){
				int ver = Integer.parseInt(rdm.responseData.getString("server_time"));
				FormObject.getInstance().setDataVersion(ver);
			}

			boolean[] upArr = FormObject.getInstance().getUpdateArray();
			boolean isOk = true;
			for (boolean bx : upArr) {
				if(bx){  			// b true means there are pending updates. App has to deal with all the pending 
					isOk = false;	// updates first in order to get the latest server updates.
					break;
				}
			}

			if(isOk){  // If no pending updates, get latest update statuses from server
				JSONObject obj1 = rdm.responseData.getJSONObject("update");
				upArr[0] = obj1.getString("stream").equals("1")? true : false;
				upArr[1] = obj1.getString("wp").equals("1")? true : false;
				upArr[2] = obj1.getString("show").equals("1")? true : false;
				upArr[3] = obj1.getString("event").equals("1")? true : false;

				FormObject.getInstance().setUpdateArray(upArr);
			}

			JSONObject obj2 = rdm.responseData.getJSONObject("stream");
			FormObject.getInstance().setSDStream(obj2.getString("sd"));
			FormObject.getInstance().setHDStream(obj2.getString("hd"));

			JSONObject strmResults = rdm.responseData.getJSONObject("server_date");
			if(strmResults != null && strmResults.length() > 0){
				FormObject.getInstance().setDay(strmResults.getString("day"));
				FormObject.getInstance().setDate(Integer.parseInt(strmResults.getString("date")));
				FormObject.getInstance().setMonth(Integer.parseInt(strmResults.getString("month")));
				FormObject.getInstance().setYear(Integer.parseInt(strmResults.getString("year")));
			}
			return "";
		} catch (Exception e) {
			return null;
		}
	}

	public static String getLoginDataModel(ResponseDataModel rdm) {
		try {
			if(rdm.responseData.getString("tnl_id") != null && rdm.responseData.getString("tnl_id") != ""){
				FormObject.getInstance().setTNLId(rdm.responseData.getString("tnl_id"));
			}
			return "";
		} catch (Exception e) {
			return null;
		}
	}

	public static List<OurShowsDataModel> getShowsDataModel(ResponseDataModel rdm) {
		List<OurShowsDataModel> dataModel = new ArrayList<OurShowsDataModel>();
		try {
			if(rdm.responseData.getString("shows") != null && rdm.responseData.getString("shows") != ""){
				JSONArray showList = rdm.responseData.getJSONArray("shows");

				for(int i=0; i < showList.length(); i++){

					OurShowsDataModel shows = new OurShowsDataModel();
					JSONObject json_data = showList.getJSONObject(i);
					shows.showName = json_data.getString("showname");
					shows.rpName = json_data.getString("rpname");

					JSONArray tmpShowDays =  json_data.getJSONArray("showday");

					for(int x=0; x < tmpShowDays.length(); x++){

						if (tmpShowDays.getString(x).equals("1"))
							shows.showDays[x] = true;
						else
							shows.showDays[x] = false;
					}

					JSONArray tmpShowTime =  json_data.getJSONArray("showtime");
					shows.showtime_start = Integer.parseInt(tmpShowTime.getString(0));
					shows.showtime_end = Integer.parseInt(tmpShowTime.getString(1));

					if(json_data.getString("imagelink") != null && !(json_data.getString("imagelink").equals("")))
						shows.imageLink = new URL(json_data.getString("imagelink"));
					else
						shows.imageLink = null;

					if(json_data.getString("backgroundlink") != null && !(json_data.getString("backgroundlink").equals("")))
						shows.backgroundLink = new URL(json_data.getString("backgroundlink"));
					else
						shows.backgroundLink = null;

					if(json_data.getString("weblink") != null && !(json_data.getString("weblink").equals("")))
						shows.webLink = new URL(json_data.getString("weblink"));
					else
						shows.webLink = null;

					dataModel.add(shows);
				}
			}
			return dataModel;
		} catch (Exception e) {
			return null;
		}
	}

	public static List<EventsDataModel> getEventsDataModel(ResponseDataModel rdm) {
		List<EventsDataModel> dataModel = new ArrayList<EventsDataModel>();
		try {
			if(rdm.responseData.getString("events") != null && rdm.responseData.getString("events") != ""){
				JSONArray eventsList = rdm.responseData.getJSONArray("events");

				for(int i=0; i < eventsList.length(); i++){

					EventsDataModel events = new EventsDataModel();
					JSONObject json_data = eventsList.getJSONObject(i);
					events.eventName = json_data.getString("name");
					events.venue = json_data.getString("venue");
					events.date = json_data.getString("date");
					events.eventtime_start = Integer.parseInt(json_data.getString("time"));

					//JSONArray tmpEventTime =  json_data.getJSONArray("time");
					//events.eventtime_start = Integer.parseInt(tmpEventTime.getString(0));
					//events.eventtime_end = Integer.parseInt(tmpEventTime.getString(1));

					if(json_data.getString("imagelink") != null && !(json_data.getString("imagelink").equals("")))
						events.imageLink = new URL(json_data.getString("imagelink"));
					else
						events.imageLink = null;

					if(json_data.getString("weblink") != null && !(json_data.getString("weblink").equals("")))
						events.webLink = new URL(json_data.getString("weblink"));
					else
						events.webLink = null;

					dataModel.add(events);
				}
			}
			return dataModel;
		} catch (Exception e) {
			return null;
		}
	}


	public static String getRequestResponseStatus(ResponseDataModel rdm) {
		try {
			if(rdm.responseData.getString("message") != null && rdm.responseData.getString("message") != ""){
				return rdm.responseData.getString("message");
			}
			return "";
		} catch (Exception e) {
			return null;
		}
	}


	public static NowPlayingDataModel getNowPlayingDataModel(ResponseDataModel rdm) {
		NowPlayingDataModel npdm = new NowPlayingDataModel();
		try {
			if(rdm == null)	return npdm;

			npdm.artistName = rdm.responseData.getString("artistname");
			npdm.songName = rdm.responseData.getString("songname");
			npdm.songDuration = Integer.parseInt(rdm.responseData.getString("songduration")) * 1000;
			npdm.albumArtImageLink = new URL(rdm.responseData.getString("imagelink"));

			FormObject.getInstance().setSongName(npdm.songName);
			FormObject.getInstance().setArtistName(npdm.artistName);
			
			return npdm;
		} catch (Exception e) {
			return npdm;
		}
	}


	public static boolean getDateTimeDataModel(ResponseDataModel rdm) {
		try {
			JSONObject strmResults = rdm.responseData.getJSONObject("server_time");
			if(strmResults != null && strmResults.length() > 0){
				String ss = strmResults.getString("hours");
				String st = strmResults.getString("mins");
				
				int svrTime = Integer.parseInt(ss + st);
				
				Calendar c = Calendar.getInstance();
				int Hr24 = c.get(Calendar.HOUR_OF_DAY);
				int Min = c.get(Calendar.MINUTE);
				String tme = Hr24 + "" + Min;
				int cltTime = Integer.parseInt(tme);
				
				int diff = cltTime-svrTime;
				FormObject.getInstance().setTimeDifference(diff * -1);
			}

			return true;
		} catch (Exception e) {
			return false;
		}
	}
}